package com.cictec.middleware.tde.rm.disp.receiver.handler;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.pojo.MiddlewarePosition;
import com.cictec.middleware.feign.distribute.MiddlewarePositionService;
import com.cictec.middleware.tde.rm.disp.annotation.HandleMessages;
import com.cictec.middleware.tde.rm.disp.receiver.AbstractReceiverHandler;
import com.cictec.middleware.tde.rm.protocol.TerminalMessage;
import com.cictec.middleware.tde.rm.protocol.rm.message.Rm0x02TerminalMessage;

/**
 * 位置信息上传，中心反馈：无【已在AutoResponseFilter中配置回复应答】
 * 
 * @author Administrator
 *
 */
@Component
@HandleMessages(0X02)
public class TerminalPositionHandler extends AbstractReceiverHandler {

	private static Logger logger = LoggerFactory.getLogger(TerminalPositionHandler.class);


	@Autowired
	private MiddlewarePositionService middlewarePositionService;

	@Override
	public void handleMessage(TerminalMessage message, IoSession session) {

		Rm0x02TerminalMessage msg = (Rm0x02TerminalMessage) message;
		
		/* ************* 测试代码 start ********************/
		boolean flag = PropertiesUtils.getBoolean("isTest", false);
		if(true){
			test(msg);
		}
		/* ************* 测试代码 end ********************/
		

		logger.debug("【0X02上传GPS定位数据】终端 {} Session {}  sendTime:{}", msg.getTerminalId(), msg.getSessionId(), DateUtils.formatDateTime(msg.getHeader().getSendTime()));
		
		MiddlewarePosition position = new MiddlewarePosition(msg.getUuid());
		position.setLineId(msg.getHeader().getLineId());
		position.setDevCode(msg.getHeader().getTerminalId()+"");
		position.setCreateTime(msg.getHeader().getSendTime());	// 位置信息没有创建时间，默认发送时间
		position.setSendTime(msg.getHeader().getSendTime());
		position.setLat(msg.getLat());
		position.setLng(msg.getLng());
		position.setAngle(msg.getAngle());
		position.setSpeed(msg.getSpeed());
		
		
		// 中间件位置信息处理入口
		int max = 200;
		int min = 1;
		Random random = new Random();
		int s = random.nextInt(max) % (max - min + 1) + min;
		System.out.println(s % 5);
		if (s % 5 == 0) {
			position.setCityCode("111111");
		} else if (s % 5 == 1) {
			position.setCityCode("222222");
		} else if (s % 5 == 2) {
			position.setCityCode("333333");
		} else if (s % 5 == 3) {
			position.setCityCode("444444");
		} else if (s % 5 == 4) {
			position.setCityCode("555555");
		}
		
		position.setCityCode("110022");
		middlewarePositionService.positionUpload(position);
		
	}
	
//	public static Date testDateTime = null;
	public static Map<String, Date> testDateTime = new HashMap<String, Date>();
	private void test(Rm0x02TerminalMessage msg){
		Date date = msg.getHeader().getSendTime();
//		date = DateUtils.getDateFromMinute(date, 80);
		String strDate = DateUtils.formatDateTimes(date);
		
		String nowDate=  DateUtils.getDate();
//		nowDate = "2017-06-08";
		strDate = nowDate + strDate.substring(10);
		msg.getHeader().setSendTime(DateUtils.parseDateTimes(strDate));
//		msg.getHeader().setSendTime(new Date());
		
//		tp.getHeader().setSendTime(DateUtils.parseDate("2017-06-27 06:00:26"));
		
		
//		if(testDateTime.get(msg.getTerminalId()) == null){
//			testDateTime.put(msg.getTerminalId(), new Date());
//		}else{
//			testDateTime.put(msg.getTerminalId(), DateUtils.getDateFromSeconds(testDateTime.get(msg.getTerminalId()), 6));
//		}
//		msg.getHeader().setSendTime(testDateTime.get(msg.getTerminalId()));
		/* ************* 测试代码 end ********************/
	}

}
